﻿using AutoMapper;
using Mysoft.Common.Extensions;
using Mysoft.Data;
using Mysoft.Domain.Basic;
using Mysoft.Dto;
using Mysoft.Entity;
using System.Linq.Expressions;
using System.Transactions;

namespace Mysoft.Domain
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public class UserDomainService : DomainBase
    {

        private readonly EntityService<Sys_User> _entityService;
        private readonly IMapper _mapper;
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="entityService"></param>
        /// <param name="mapper"></param>
        public UserDomainService(EntityService<Sys_User> entityService, IMapper mapper)
        {
            _entityService = entityService;
            _mapper = mapper;
        }
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserDto>> getUsers()
        {
            var users = await _entityService.Instance.FindAsync(m => true);
            //实体转dto
            var userDto = _mapper.Map<List<UserDto>>(users);
            //dto转实体
            var userEntity = _mapper.Map<List<Sys_User>>(userDto);
            return userDto;
        }

        /// <summary>
        /// 更新Token
        /// </summary>
        /// <returns></returns>
        public  void  ChangeToken(Guid id,string token) 
        {
            var user = _entityService.Instance.FindFirst(m => m.Id == id);
            if (user != null) {
                user.Token = token;
                _entityService.Instance.Modify(user);
            }
        }
        /// <summary>
        /// 根据账号密码获取用户
        /// </summary>
        /// <param name="code"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public Sys_User Login(string code,string pwd)
        {
            var user =  _entityService.Instance.FindFirst(m => m.Code == code && m.Password == pwd);
            return user;
        }
        /// <summary>
        /// 获取当前登录的用户
        /// </summary>
        /// <returns></returns>
        public UserDto GetUser()
        {
            var user = _entityService.Instance.FindFirst(_UserContext.UserId.Value);
            var userDto = _mapper.Map<UserDto>(user);
            userDto.Token = null;
            userDto.Password = null;
            return userDto;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="dto"></param>
        public async void addUser(UserDto dto)
        {
          
            //dto转实体
            var user = _mapper.Map<Sys_User>(dto);
            var _cacheUser = base._UserContext.UserInfo;
            user.CreateGuid = _cacheUser.Id;
            user.CreateName = _cacheUser.Name;
            user.CreateDate = DateTime.Now;
            user.UpdateGuid = _cacheUser.Id;
            user.UpdateName = _cacheUser.Name;
            user.UpdateDate = DateTime.Now;
            _entityService.Instance.AddAsync(user);
        }
        public void modifyUserName(string oldName, string newName)
        {
            //动态表达式拼接
            Expression<Func<Sys_User, bool>> where = LambdaExtensions.True<Sys_User>();
            if (!string.IsNullOrWhiteSpace(oldName)) where = where.And(m => m.Name.Contains(oldName));
            var user = _entityService.Instance.FindFirst(where);

            user.Name = newName;

            _entityService.Instance.Modify(user);
        }
    }
}